/*-------------------------------------------------------------------------
	created:	2010/09/16  17:50
	filename: 	e:\Project_SVN\Server\GamesScene\GameScene.cpp
	author:		Deeple
	
	purpose:	
---------------------------------------------------------------------------*/

#include "ModMail.h"
#include "async_func.h"
#include "IndexManage.h"


/*
 * ***********************************************
 * 不用这个文件
 * *************************************************
 */

IGlobal* g_pGame = NULL;

void RegisterCallBack()
{
	REG_PUBLIC_MESSAGE(GM_SEND_MAIL);			 // 发送邮件
	REG_PUBLIC_MESSAGE(GM_MAIL_LIST);			 // 获取邮件列表
	REG_PUBLIC_MESSAGE(GM_READ_MAIL);			 // 查看邮件
	REG_PUBLIC_MESSAGE(GM_DELETE_MAIL);			 // 删除邮件
	REG_PUBLIC_MESSAGE(GM_GET_MAIL_ATTACHMENT);  // 获取邮件附件

	// 载入位置信息
	g_IndexFinder.LoadLocation(g_pGame);
}

// 发送邮件
void PUB_MSG(GM_SEND_MAIL)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
//	const char* Title = pmsg->StringVal();
//	const wchar_t* Sender = pmsg->WideStrVal();
//	const wchar_t* ReceName = pmsg->WideStrVal();
//	const char* MailAttach = pmsg->StringVal();
//	const char* MailContent = pmsg->StringVal();
//
//	// 普通邮件
//	if (wcslen(ReceName) > 0)
//	{
//		// 给发送者写入邮件
//		if (wcslen(Sender) > 0)
//		{
//			CMsgTyped SendMail;
//			SendMail.SetInt(receiver);
//			SendMail.SetInt(msgId);
//			SendMail.SetInt((int)id.nIndex);
//			SendMail.SetInt((int)id.nIdent);
//
//			SendMail.SetWideStr(Sender);
//			SendMail.SetString(Title);
//			SendMail.SetWideStr(Sender);
//			SendMail.SetWideStr(ReceName);
//			SendMail.SetString(MailAttach);
//			SendMail.SetString(MailContent);
//
//			// 位置信息
//			UI32 PoolId = g_IndexFinder.FindRole(Sender);
//
//			g_pGame->Call(PoolId , AsyncProc_send_mail, &SendMail);
//		}
//
//		// 给接收者写入邮件
//		{
//			CMsgTyped SendMail;
//			SendMail.SetInt(receiver);
//			SendMail.SetInt(msgId);
//			SendMail.SetInt((int)id.nIndex);
//			SendMail.SetInt((int)id.nIdent);
//
//			SendMail.SetWideStr(ReceName);
//			SendMail.SetString(Title);
//			SendMail.SetWideStr(wcslen(Sender) > 0 ? Sender : L"系统");
//			SendMail.SetWideStr(ReceName);
//			SendMail.SetString(MailAttach);
//			SendMail.SetString(MailContent);
//
//			// 位置信息
//			UI32 PoolId = g_IndexFinder.FindRole(ReceName);
//
//			g_pGame->Call(PoolId , AsyncProc_send_mail, &SendMail);
//		}
//	}
//	else
//	{
//		// 群发邮件
//		CMsgTyped SendMail;
//		SendMail.SetInt(receiver);
//		SendMail.SetInt(msgId);
//		SendMail.SetInt((int)id.nIndex);
//		SendMail.SetInt((int)id.nIdent);
//
//
//		SendMail.SetString(Title);
//
//		SendMail.SetString(MailAttach);
//		SendMail.SetString(MailContent);
//
//		// 位置信息
//		UI32 PoolId = g_IndexFinder.FindRole(ReceName);
//
//		//. 获取集群的节点列表
//		int poolCount[200];
//		int count = 200;
//		g_pGame->QueryPoolList(2, poolCount, count);
//
//		for (int i=0; i < 200; i++)
//		{
//			if (poolCount[i] == 0) break;
//			g_pGame->Call(poolCount[i] , AsyncProc_send_mailAll, &SendMail);
//		}
//	}
	
}

// 获取邮件列表
void PUB_MSG(GM_MAIL_LIST)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	_Debug("PUB_MSG(GM_MAIL_LIST)");

	CMsgTyped MailList;

	const wchar_t* PlayerName = pmsg->WideStrVal();
	MailList.SetInt( receiver );
	MailList.SetInt(msgId);
	MailList.SetInt((int)id.nIndex);
	MailList.SetInt((int)id.nIdent);
	MailList.SetWideStr(PlayerName);
	
	//std::cout<<"邮件列表已获取！"<<std::endl;
	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_mail_list, &MailList);
}

// 查看邮件
void PUB_MSG(GM_READ_MAIL)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	_Debug("PUB_MSG(GM_READ_MAIL)");

	CMsgTyped ReadMail;
	const char* MailID = pmsg->StringVal();
	const wchar_t* PlayerName = pmsg->WideStrVal();

	ReadMail.SetInt(receiver);
	ReadMail.SetInt(msgId);
	ReadMail.SetInt((int)id.nIndex);
	ReadMail.SetInt((int)id.nIdent);
	ReadMail.SetString(MailID);
	
	//std::cout<<"邮件已经查看！"<<std::endl;
	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_read_mail, &ReadMail);
}

// 删除邮件
void PUB_MSG(GM_DELETE_MAIL)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	_Debug("PUB_MSG(GM_DELETE_MAIL)");

	CMsgTyped DeleteMail;

	const char* MailID = pmsg->StringVal();
	const wchar_t* PlayerName = pmsg->WideStrVal();

	DeleteMail.SetInt(receiver);
	DeleteMail.SetInt(msgId);
	DeleteMail.SetInt((int)id.nIndex);
	DeleteMail.SetInt((int)id.nIdent);
	DeleteMail.SetString(MailID);
	DeleteMail.SetWideStr(PlayerName);

	//std::cout<<"邮件已删除！"<<std::endl;
	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_delete_mail, &DeleteMail);
}

// 获取邮件附件
void PUB_MSG(GM_GET_MAIL_ATTACHMENT)::OnIoMessage(PersistID id, UI32 receiver, UI32 msgId, CMsgTyped* pmsg)
{
	_Debug("PUB_MSG(GM_GET_MAIL_ATTACHMENT)");

	const char* MailID = pmsg->StringVal();
	const wchar_t* PlayerName = pmsg->WideStrVal();

	CMsgTyped Get_Attachment;
	
	Get_Attachment.SetInt(receiver);
	Get_Attachment.SetInt(msgId);
	Get_Attachment.SetInt((int)id.nIndex);
	Get_Attachment.SetInt((int)id.nIdent);
	Get_Attachment.SetString(MailID);
	Get_Attachment.SetWideStr(PlayerName);

	//std::cout<<"已获取邮件附件！"<<std::endl;
	g_pGame->Call(g_IndexFinder.FindRole(PlayerName), AsyncProc_mail_attachment, &Get_Attachment);
}

